/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.campaign.service.impl.CampaignProductServiceImpl
   Module Description   :

   Date Created      : 2010/1/4
   Original Author   : jeff.ma
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.campaign.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.impl.DomainServiceImpl;
import com.yaodian100.ec.campaign.entity.CampaignProduct;
import com.yaodian100.ec.campaign.service.CampaignProductService;

/**
 * @author jeff.ma
 * 
 */
public class CampaignProductServiceImpl extends DomainServiceImpl<CampaignProduct> implements CampaignProductService {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.campaign.service.CampaignProductService#getProductsByOrderPrice(java.math.BigDecimal,
	 * java.lang.String, int)
	 */
	public List<CampaignProduct> getProductsByOrderAmount(BigDecimal orderAmount, String memberRank, int returnSize) throws CoreException {
		Date now = new Date(System.currentTimeMillis());
		CommonCriteria criteria = new CommonCriteria();
		criteria.addLe("minOrderAmount", orderAmount);
		criteria.addEq("enable", true);
		criteria.addLe("startDateTime", now);
		criteria.addGe("endDateTime", now);
		if (StringUtils.isNotEmpty(memberRank)) {
			criteria.addLe("allowMemberRank", memberRank);
		}
		return getList(0, returnSize, criteria, null);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.core.common.service.impl.DomainServiceImpl#delete(java.lang.Object)
	 */
	@Override
	@Transactional(readOnly = false)
	public void delete(CampaignProduct entity) throws CoreException {
		entity.setDelete(true);
		getDao().update(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.core.common.service.impl.DomainServiceImpl#getList(int, int,
	 * com.yaodian100.core.common.dao.impl.CommonCriteria, java.lang.String[])
	 */
	@Override
	public List<CampaignProduct> getList(int firstResult, int maxResults, CommonCriteria criteria, String[] sortOrder) throws CoreException {
		if (criteria == null) {
			criteria = new CommonCriteria();
		}
		criteria.addEq("delete", false);
		return super.getList(firstResult, maxResults, criteria, sortOrder);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.yaodian100.core.common.service.impl.DomainServiceImpl#getListSize(com.yaodian100.core.common.dao.impl.
	 * CommonCriteria)
	 */
	@Override
	public Number getListSize(CommonCriteria criteria) throws CoreException {
		if (criteria == null) {
			criteria = new CommonCriteria();
		}
		criteria.addEq("delete", false);
		return super.getListSize(criteria);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.core.common.service.impl.DomainServiceImpl#getAll(int, int, java.lang.String[])
	 */
	@Override
	public List<CampaignProduct> getAll(int firstResult, int maxResults, String[] sortOrder) throws CoreException {
		return getList(0, -1, null, sortOrder);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.core.common.service.impl.DomainServiceImpl#getAllSize()
	 */
	@Override
	public Number getAllSize() throws CoreException {
		return getListSize(null);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.campaign.service.CampaignProductService#getProduct(java.lang.String)
	 */
	@Override
	public CampaignProduct getProduct(String productId) throws CoreException {
		Date now = new Date(System.currentTimeMillis());
		CommonCriteria criteria = new CommonCriteria();
		criteria.addEq("enable", true);
		criteria.addLe("startDateTime", now);
		criteria.addGe("endDateTime", now);
		criteria.addGe("productId", productId);

		List<CampaignProduct> products = getList(0, 2, criteria, null);
		if (products != null && products.size() > 0) {
			if (products.size() > 1) {
				logger.warn("product size more than 1, productId;{}", productId);
			}
			return products.get(0);
		}
		return null;
	}

}
